# Object data reduction
#
# Use "setintrument ccd1k|ccd2k" to set default parameters for ccdproc and dispcor
#
# Created by Cesar Strauss
# Modified in 06/03/2002 by Cesar Strauss: added comments
# Modified in 08/09/2002 by Cesar Strauss: added various boolean options

procedure ifu ( )

string image {"obj0001",prompt="Input frame"}
string trref {"DomeFlat",prompt="Fiber transmission calibration - unmasked flat"}
string imref {"near0001.ql",prompt="Calibration arc reference"}
string apref {"mask",prompt="Fiber apertures reference"}
bool   ccdcor {"yes",prompt="Run ccdproc on input images ? (yes/no)"}
bool   extract {"yes",prompt="Extract fibers ? (yes/no)"}
bool   ql    {"yes",prompt="Quick look ? (yes/no)"}
bool   tcor  {"yes",prompt="Transmission correction? (yes/no)"}
bool   dcor  {"yes",prompt="Dispersion correction? (yes/no)"}
bool   gencube {"yes",prompt="Generate datacube ? (yes/no)"}
bool   ldisplay {"yes",prompt="Display reconstructed image ? (yes/no)"}

pset ccdproc {prompt="ccdproc parameters (use :e to edit)"}
pset dispcor {prompt="dispcor parameters (use :e to edit)"}

begin

string image_file,fiber_file,ref_file,ap_file
string n_trref
bool n_ql, n_dcor, n_tcor

image_file = image
ref_file = imref
ap_file = apref
fiber_file = image_file
n_trref = trref

n_ql = ql
n_dcor = dcor
n_tcor = tcor

# Trim the image
# Apply zero level and overscan corrections
if (ccdcor == yes ) {
  print("Trimming and applying zero level and overscan corrections...")
  ccdproc (image_file)
}

# extraction
if (extract == yes ) {
  # by aperture
  if ( ql == yes ) {
      print("Extracting apertures from input image...")
      fiber_file = image_file // ".ql"
      print("Using file = ",ap_file)
      apsum(image_file,output=fiber_file,reference=ap_file) 
      }
  # by linear gaussian fitting
  else {
      print("Extracting gaussians from input image...")
      fiber_file = image_file // ".lin"
      print("A")
      fibint(input=image_file)
      print("B")
      }
}

# Transmission correction
if ( n_tcor == yes ) {
   print ("Transmission correction...")
   transcor(input=fiber_file,output=fiber_file//".tc",imcor=n_trref)
   fiber_file = fiber_file // ".tc" 
}

# Wavelenght calibration
if ( n_dcor == yes ) {
   print ("Wavelenght calibration...")
   hedit (fiber_file,"REFSPEC1",ref_file,add+,upd+,del-,ver-,show-)
   dispcor(fiber_file,fiber_file//".wl")
   fiber_file = fiber_file // ".wl"
}

# Datacube conversion
if (gencube == yes) {
  print("Generating datacube....")
  datacube(fiber_file,fiber_file//".dc")
print("C0")
}

# Display the reconstructed image
if (ldisplay == yes) {
print("C")
  ldisplay(fiber_file,output=fiber_file//".ldisp",w1=indef,w2=indef)
}
end
